// JavaScript Document

function showuserlist()
{
    var userlist = $("#userlist");
    var con = $("#userlist .con");
    var leftpx = (document.body.offsetWidth - 560) / 3;
    var toppx = (document.body.offsetHeight - 170) / 4 + document.body.scrollTop;
    userlist.css({display:"block",left:leftpx,top:toppx});

    if(con.html() == "")
    {
        refresh_user_list();
    }    
}

function getfxintro(aid)
{
		var intro = $(".fx-intro");
		intro.html("loading...");
		
		setTimeout(function(){return getfxintro_action(aid)},1000);
}

function getfxintro_action(aid)
{
		$.get("index.php",{action:'ajax_getfxlist',id:aid},function(data){
				$(".fx-intro").html(data);
		});
}


function refresh_user_list()
{
    var con = $("#userlist .con");

    con.ajaxStart(function(){
        $(this).html("正在載入列表...");
    });
    
    $.get("index.php",{view:'ajax_getuserlist'},function(data){
        con.html(data);
    });
}

function confirm()
{
    var htmlstr = '';
    var to = $("#to");
    var mailto = $("#mailto");
    var checkbox = $("input:checked");
    
    checkbox.each(function(i){
        if($(this).attr("checked")) {
            htmlstr = htmlstr + $(this).val() + ";";
        }
    });
    
    htmlstr = htmlstr.slice(0,-1);
    closealert();
    if(htmlstr == "")
    {
        to.html("點擊此處選擇收信人");
    }
    else
    {
        to.css("line-height","25px").html(htmlstr);
        mailto.val(htmlstr);
    }
}

function showtestmail()
{
    var testmail = $("#testmail");
    testmail.css("display","block");
}

function sendtestmail()
{
    var mailadds = $("#mailadds"),msgbox = $("#msgbox"),sendmail = $("#sendmail");
    if(mailadds.val() == '')
    {
        msgbox.css("color","red").html("請輸入收信帳戶！");
    }
    else
    {
        sendmail.attr("disabled",true);

//        msgbox.ajaxStart(function(){
//            $(this).html("正在發送...")
//        });
//mailto:mailadds,smtpserver:$("#smtpserver"),smtpacount:$("#smtpacount"),smtppass:$("#smtppass"),smtpport:$("#smtpport")
        $.get("index.php",{action:'testsendmail'},function(data){
            if(data == 'Success')
            {
                msgbox.css("color","#799AE1").html("測試郵件已發送至收信帳戶");
            }
            else if(data == 'Failure')
            {
                msgbox.css("color","red").html("郵件發送未能成功，請檢查收信帳戶或SMTP設定是否正確");
            }
        });
    }
}

function closealert()
{
    $("#userlist").css("display","none");
}

function verification_check(formname)
{
    switch(formname)
    {
        case 'sendmail':
            if($("#mailto").val() == "")
            {
                alert('請選擇收信人！！');
                return false;
            }
            else if($("#subject").val() == "")
            {
                alert('請輸入寄信主題!!');
                return false;
            }
            break;
    }
}

function changepass_verification()
{
	if($("#oldpass").val() == ""){
		alert('請填寫舊密碼!!');
		$("#oldpass").focus();
		return false;
	}else if($(".newpass").val() == ""){
		alert('請填寫新密碼!!');
		$("#newpass").focus();
		return false;
	}else if($("#chkpass").val() == ""){
		alert('請再次填寫新密碼!!');
		$("#chkpass").focus();
		return false;
	}else if($("#newpass").val() !== $("#chkpass").val()){
		alert('新密碼兩次輸入不一致!!');
		return false;
	}
	else
	{
		return true;
	}
}

function changeform()
{
	var category=$("#category"),
		fbody=$("#fbody"),
		fileurl = "/admin/?view=newsadd&parameter=" + category.val() + "";
	window.location.href=(fileurl);
}

function selectAll()
{
	var checkbox = $("input[type=checkbox]");
	checkbox.attr("checked","checked");
}

function conversely()
{
	var checkbox = $("input[type=checkbox]");
	checkbox.attr("checked",false);
}

function addfileinput()
{
    var uploadbox = $("#uploadbox"),htmlstr = "<ul><li class='left'>請選擇要執行的文件：</li><li class='right'><input type='file' name='upfiles[]' /></li></ul>";
    uploadbox.append(htmlstr);
}

function showimg(url)
{
	var imgbox = $("#imgbox");
    var leftpx = (document.body.offsetWidth - 560) / 2;
    var toppx = (document.body.offsetHeight - 170) / 4 + document.body.scrollTop;
	imgbox.css({display:"block",left:leftpx,top:toppx}).html("<img src='' id='img' border='0' alt='close' width='500' onclick='closebox()' style='cursor:pointer' />");
    $("#img").attr("src",url);
}

function closebox()
{
	var imgbox = $("#imgbox");
	imgbox.css({display:"none"});
}